// pages/image-cropper/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    src: null,
    size: {
      width: 210 * 3,
      height: 297 * 3
    },
    cropSizePercent: 0.9,
    borderColor: '#fff',
  },
  onLoad: function (option) {
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    const _this = this
    const eventChannel = _this.getOpenerEventChannel()
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      if (data.cropScale) {
        const scale = data.cropScale.split(":")
        _this.setData({
          size: {
            width: scale[1],
            height: scale[0]
          }
        })
      } else {
        _this.setData({
          size: {
            width: 297 * 3,
            height: 210 * 3
          }
        })
      }
      _this.setData({
        src: data.imagePath
      })
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.preloadWebview()
  },
  //选取裁剪图片成功回调
  uploadCallback: function (event) {
    console.log(event);
  },

  //裁剪图片回调
  cropCallback: function (event) {
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('acceptDataFromOpenedPage', { tempFilePath: event.detail.resultSrc, cropParam: event.detail.cropParam })
    wx.navigateBack({
      delta: -1
    })
  },

  //关闭回调
  closeCallback: function (event) {
    const eventChannel = this.getOpenerEventChannel()
    if (eventChannel) {
      this.getOpenerEventChannel().emit('acceptDataFromOpenedPage', { exit: true })
    }
    wx.navigateBack({
      delta: -1
    })
  }
})